package servlet;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Offer;
import model.Trade;
import persistence.TradeDao;

/**
 * Servlet implementation class TaskContent
 */
@WebServlet("/TaskContent")
public class TaskContent extends MyHttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	//TODO Connect the Database & Remove temp implement
	private void process(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			printWriter = response.getWriter();
			response.setContentType("application/xml");
			printWriter.write("<content>");

			String taskId = null;
			Cookie[] cookies = request.getCookies();
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = cookies[i];
				if (cookie.getName().equals("taskId")) {
					taskId = cookie.getValue();
				}
			}

			printWriter.write("<taskId>" + taskId + "</taskId>");
			
//			//TEMP IMPLEMENT
//			printWriter.write("<post>");
//			addTag("id", "345678123");
//			addTag("date", "2013-08-09");
//			addTag("body", "Complete 1000 Exercise before the end of world.");
//			addTag("reward", "Excalibur");
//			addTag("author", "etenaelta");
//			addTag("image","userimages\\8919982331744603747.jpg");
//			addTag("status", "no");
//			printWriter.write("</post>");
//			
//			printWriter.write("<entries>");
//
//			printWriter.write("<entry>");
//			addTag("id", "547896321");
//			addTag("date", "2013-02-01");
//			addTag("body", "I can finish it as well as you.");
//			addTag("author", "shadow_sausage");
//			addTag("image", "");
//			addTag("type", "waiting");
//			printWriter.write("</entry>");
//			
//			printWriter.write("<entry>");
//			addTag("id", "547896321");
//			addTag("date", "2013-02-01");
//			addTag("body", "");
//			addTag("author", "burger");
//			addTag("image", "");
//			addTag("type", "waiting");
//			printWriter.write("</entry>");
//
//			printWriter.write("<entry>");
//			addTag("id", "547896321");
//			addTag("date", "2013-02-01");
//			addTag("body", "");
//			addTag("author", "kebab");
//			addTag("image", "");
//			addTag("type", "waiting");
//			printWriter.write("</entry>");
//			
//			printWriter.write("<entry>");
//			addTag("id", "547896321");
//			addTag("date", "2013-02-01");
//			addTag("body", "");
//			addTag("author", "puff");
//			addTag("image", "");		
//			addTag("type", "failed");			
//			printWriter.write("</entry>");
//			//TEMP IMPLEMENT END
			
			Trade trade = TradeDao.load(Long.parseLong(taskId));
			
			printWriter.write("<post>");
			addTag("id", String.valueOf(trade.getId()));
			addTag("date", trade.getDate().toString());
			addTag("body", trade.getState());
			addTag("reward", String.valueOf(trade.getHighPrice()));
			addTag("author", trade.getOwner().getName());
			addTag("image",trade.getOwner().getStatus());
			addTag("status", trade.getMode()==0?"no":"yes");
			printWriter.write("</post>");
			
			printWriter.write("<entries>");
			@SuppressWarnings("unchecked")
			Set<Offer> offerSet = trade.getSolution();
			Iterator<Offer> it = offerSet.iterator();
			
			while(it.hasNext()) {
				printWriter.write("<entry>");
				Offer buffer = it.next();
				addTag("id", String.valueOf(buffer.getId()));
				addTag("date", buffer.getDate().toString());
				addTag("body", buffer.getSolution());
				addTag("author", buffer.getSeller().getName());
				addTag("image", buffer.getSeller().getStatus());
				switch(buffer.getSelected()) {
				case 0:
					addTag("type","waiting");
				case -1:
					addTag("type","failed");
				case 1:
					addTag("type","pass");
				}
				printWriter.write("</entry>");
			}
			
			printWriter.write("</entries>");
			printWriter.write("</content>");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
